package engine.ui
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.RoleVo;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.newUi.ActorPropertyUI;
	import engine.tip.TipsMainLogic;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.tab.NxTab;
	import newx.com.control.tab.NxTabEvent;

	/**
	 *玩家及其伙伴列表 逻辑类 
	 * @author yanghr
	 * 
	 */
	public class ActorPartnerGroup
	{
		private const DATAOBJ:String="dataobj";
		private const INDEX:String="index";
		private const EQUIP:String="item";
		private const TXT:String="titleTxt2";
		private const LVL:String="infoTxt5";
		private const BIND:String="bind";
		
		private const CONFIGID:String="ConfigID";
		private const BIND_LEVEL:String="bind_level";
		private const IS_EMBATTLE:String="_isEmbattle"
		private var _container:DisplayObject;
		private var _actorGroup:NxViewGroup;
		private var _actorTab:NxTab;

		public function get actorGroup():NxViewGroup
		{
			return _actorGroup;
		}
		private var setMCcallBack:Function; 
		
		
		private var _roleArr:Array;

		//阵法相关
		private  var _tabArr:Array=[];
		private var _partnerUidMap:Object={};
		private var _upbattle:int;
		
		private var _bindEvokeFlag:int=-1;
		private var _bindFlag:int=-1;
		private var _blindFlag:int=-1;
		
		private var _initBeforeShow:Boolean;
		private var _isPartnerNotEmbattle:Boolean;
		/****/
		private var _clickCallBack:Function;
		/**
		 * 
		 * @param container 容器
		 * @param setMc   按钮单击回调
		 * @param refreshlist   刷新回调
		 * 
		 */		
		public function ActorPartnerGroup(
			container:DisplayObject,
			setMc:Function			=	null,
			refreshlist:Function	=	null,
			clickcallBack:Function 	= 	null)
		{
			this.container=container;
			setMCcallBack=setMc;
			partnerChangeCallBack=refreshlist;
			_clickCallBack = clickcallBack;
			init();
		}
		public function set container(value:DisplayObject):void 
		{
			_container=value;
			if(_container is NxViewGroup)
			{
				_actorGroup=_container as NxViewGroup;
				if(actorGroup)
				{
					_currentMC=actorGroup.getItemByIndex(0);
					_currentMC.gotoAndStop(3);
				}
			}
			else
			{
				_actorTab=_container as NxTab;
				_currentMC=new MovieClip;
			}
		}
		private function init():void
		{
			Init();
			resetRoleArr();
			if(_actorGroup)
			{
				initPartnerList();
			}
		}
		private function resetRoleArr():void
		{
			_roleArr=new Array();
			_roleArr[0]=_role;
		}
		public function get rollArr():Array
		{
			return _roleArr;
		}
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		private function  Init():void
		{
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
		}
		/**
		 *初始化伙伴列表 
		 * 
		 */		
		private function initPartnerList():void
		{
			var len:int		=	_actorGroup.col*_actorGroup.row
			//伙伴列表
			for (var i:int = 0; i <len ; i++) 
			{
				var mc:MovieClip	=	_actorGroup.getItemByIndex(i);
				//				mc[TXT]		=	new NxLabel();
				if(mc.hasOwnProperty(TXT))
					mc[TXT].mouseEnabled	=false
				//				mc.addChild(mc[TXT]);
				mc[INDEX]	=	i;
			}
		}
		public function	BeforeShow():void
		{
			_initBeforeShow=true;
			addAllEventListener();
			refreshList();
			_initBeforeShow=false;
		}
		public function	AfterHide():void
		{
			removeAllEventListener();
			if(actorGroup)		removeAllMcBlind();
		}
		public function addAllEventListener():void
		{
			if(actorGroup)	{
					actorGroup.addEventListener(MouseEvent.CLICK,actorClickHandle,true);
			}
			if(_actorTab)
			{
				_actorTab.		addEventListener(	NxTabEvent.TAB_CLICK, 		tabClickHandle);
			}
			if(_bindEvokeFlag==-1)
			{
				_bindEvokeFlag	=	_bind.BindProp(_model.scene.Role,"LineupID",this,"upBattle");
			}
			if( _bindFlag == -1 )
			{
				resetRoleArr();
				_bindFlag		=	_bind.BindView(_model.scene,View.VIEWPORT_PARTNER,OnAddPartner,OnRemovePartner);
				
			}
			if(_blindFlag==-1)
			{
				//				trace("EmbattleControl.addAllListener()阵法绑定");
				_blindFlag= _bind.BindRecord(_model.scene,"LineupRecord",onaddPerHandle,onremovePerHandle);
			}
		}
		/**
		 *选择伙伴 
		 * @param e
		 * 
		 */		
		private function actorClickHandle(e:Event):void
		{
			var target:DisplayObject=e.target as DisplayObject
			if(e.target is TextField)
			{
				target=target.parent;
			}
//			//			currentSelected=proUI.actorGroup.geti
//			if(target==currentMC||!_roleArr[target[INDEX]])return ;
			
			currentMC=target as MovieClip;
		}
	
		/**
		 * 
		 * @param e
		 * 
		 */
		private function tabClickHandle(e:NxTabEvent):void
		{
			_currentMC[DATAOBJ]	=	e.item["data"];
			_currentMC[INDEX]	=	(_container as NxTab).selected;
			
			currentMC=_currentMC
		}
		public function removeAllEventListener():void
		{
			if( _bindFlag != -1 )
			{
				_bindFlag		=	_bind.UnbindView(View.VIEWPORT_PARTNER,_bindFlag);
				
			}
			if(_bindEvokeFlag!=-1)
				_bindEvokeFlag	=_bind.UnbindProp(_model.scene.Role,"LineupID",_bindEvokeFlag);
			if(_blindFlag!=-1)
				_blindFlag= _bind.UnbindRecord("LineupRecord",_blindFlag);
		}
		
		private var _currentMC:MovieClip;
		
		public function get currentMC():MovieClip 
		{ 
			return _currentMC; 
		}
		public function set currentMC(value:MovieClip):void
		{
			
			if (_currentMC == value)
			{
				if(setMCcallBack!=null)
				{
					setMCcallBack(value);
				}
				if(_clickCallBack != null)
				{//选中当前的人物
					_clickCallBack.call();
				}
				
				return;
			}
			
			if(_currentMC)
			{
				_currentMC.gotoAndStop(1);
			}
			_currentMC = value;
			if(_currentMC)
			{
				_currentMC.gotoAndStop(3);
				//				_currentSelectedM.filters=[new GlowFilter()]
			}
			if(setMCcallBack!=null)
			{
				setMCcallBack(value);
			}
			if(_clickCallBack != null)
			{//选中当前的人物
				_clickCallBack.call();
			}
			
		}
		public function get currentIndex():int
		{
			return _currentMC[INDEX];
		}
		public function get currentData():ObjectData
		{
			return _currentMC[DATAOBJ]
		}
		private function BindFunc(PropertiesName:String,PropertiesValue:*,pList:Array):void
		{
			var		Obj:Object	=	pList[0];
			var		value:*		=	pList[1];
			Obj[value](PropertiesValue);	
		}
		private function OnAddPartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			_roleArr.push(PropertiesValue);
			if(PropertiesValue.ObjKey)
			{
				_partnerUidMap[PropertiesValue.ObjKey]=PropertiesValue;
			}
			if(PropertiesValue.ObjKey==currentMC[INDEX]&&!_initBeforeShow)
			{
//				refreshBlind();
			}
			if(!_initBeforeShow)
				refreshList();
		}
		//删除可能一辈子都不会呗调用吧		
		private function OnRemovePartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			_roleArr.splice(_roleArr.indexOf(PropertiesValue),1);
			//			[PropertiesValue.ObjKey]=null;
			if(PropertiesValue.ObjKey==currentMC[INDEX])
			{
//				refreshBlind();
			}
			if(!_initBeforeShow)
				refreshList()
		}
		/**
		 *当前可见长度 
		 */		
		private var _visibleLen:int;
		/**
		 *刷新伙伴列表 
		 * 
		 */		
		private function refreshList():Boolean
		{
			if(partnerChangeCallBack!=null)
				partnerChangeCallBack();
			if(!actorGroup||!actorGroup is NxViewGroup)return false;
			var view:ObjectData
			var b:Boolean=true;
			var $this:Object=this;
			var selectIndex:int=currentMC[INDEX]
			//重置 好友列表
			if(_roleArr.length	!=	_visibleLen)
			{
				for (var j:int = 0; j < actorGroup.row; j++) 
				{
					actorGroup.getItemByIndex(j).visible=j<_roleArr.length;
				}
				_visibleLen=_roleArr.length;
				b= false;
			}
			_isPartnerNotEmbattle=false;
			for (var i:int = 0; i < _roleArr.length; i++) 
			{
				view=_roleArr[i];
				setMc(view);
			}
			currentMC=actorGroup.getItemByIndex(Math.min(selectIndex,_roleArr.length-1));
			//			setMc(view)
			//				ui.actorGroup.row
			return b;
		
		}
		private function onaddPerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			var tabCount:int		=	PropertiesValue[0]-1;
			var countInGroup:int	=	PropertiesValue[1];
			var uid:String		= 	PropertiesValue[2];
			var dd:Object;
			if( _tabArr[tabCount]	==	null)
			{
				_tabArr[tabCount]	=	[];
			}
			if(uid&&uid!="0")
			{
				if(uid	==	"-1")
				{
					dd	=	 _tabArr[tabCount][countInGroup]	=	_role;
				}
				else
				{
					dd	=	 _tabArr[tabCount][countInGroup]	=	_partnerUidMap[uid];
				}
				
			}
			else
			{
				_tabArr[tabCount][countInGroup]	=	null;
			}
			if(tabCount==_upbattle-1)
			{
				rangeRole()
				if(!_initBeforeShow)
					refreshList();
			}
		}
		private function onremovePerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			//			trace("Error~MyPropertyLogic.onremovePerHandle(PropertiesName, PropertiesValue, data)","这怎么可能被调用");
		}
		/**
		 *启用阵法 
		 * @param index
		 * 
		 */		
		public function set upBattle(index:int):void
		{
			if(index==0)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHANGE_LINEUP_ID,1);
				return ;
			}
			_upbattle=index;
		}
		public function get isPartnerNotEmbattle():Boolean
		{
			return _isPartnerNotEmbattle
		}
		/**
		 *伙伴数量或者上阵 改变回调 
		 */		
		public var partnerChangeCallBack:Function;
		/**
		 *设置数据接口 
		 */		
		public var setMc:Function=	function (viewObj:ObjectData):void
		{
			if(!viewObj)return ;
			var indexxx:int=uint(_roleArr.indexOf(viewObj))
			var mc:MovieClip	=	actorGroup.getItemByIndex(indexxx)
			mc[TXT].mouseEnabled	=	false;
			if(viewObj.GetPropertiesEx(IS_EMBATTLE))
			{
				mc[TXT].htmlText	=	getImbattleStr(viewObj is ViewObject?StaticIni.getSwfLangStr(viewObj.GetPropertiesEx("ConfigID")):viewObj.GetProperties("Name"));
			}
			else
			{
				_isPartnerNotEmbattle=true;
				mc[TXT].htmlText	=getUnimbattleStr(viewObj is ViewObject?StaticIni.getSwfLangStr(viewObj.GetPropertiesEx("ConfigID")):viewObj.GetProperties("Name"));
			}
			mc[DATAOBJ]=viewObj;
			if(mc[BIND_LEVEL]==null)
			{
				mc[BIND_LEVEL]=function (lvl:String):void{
					var lvlstr:String="lv."+(lvl&&lvl!="0"?lvl:1);
					if(this[DATAOBJ].GetPropertiesEx(IS_EMBATTLE))
					{
						this[LVL].htmlText	=getLvImbattleStr(lvlstr)	
					}
					else
					{
						this[LVL].htmlText	=	getUnimbattleStr(lvlstr)
					}
				}
			}
			//				mc[BIND]=_blind.BindProp(viewObj,"Level",$this,"text"+indexxx);
			if(viewObj.CheckProperties("Level")&&indexxx<=10)
			{
				if(viewObj is RoleVo)
				{
					mc[BIND]	=_bind.BindPropInSceneObj(viewObj,"Level",BindFunc,null,[mc,BIND_LEVEL]);
				}
				else
				{
					mc[BIND]	=_bind.BindPropInViewObj(viewObj.parent as View,viewObj.ObjKey,"Level",BindFunc,null,[mc,BIND_LEVEL]);
				}
			}
			else
			{
				//					trace("Error,MyProperty，没有Level 这个属性！！！！！！！！！！！！！！！！！！！！！！！！！viewObj	=	",indexxx,viewObj);
			}
		}
		/**
		 *得到上阵的名字字符串类型 
		 * @param name
		 * @return 
		 * 
		 */			
		public function getImbattleStr(name:String):String
		{
			return _tip.getColorHtml(name,"c5ffdc");
		}
		/**
		 *得到上阵等级的字符串 
		 * @param name
		 * @return 
		 * 
		 */		
		public function getLvImbattleStr(name:String):String
		{
			return _tip.getColorHtml(name,"1eff00");
		}
		/**
		 *得到未上阵的字符串类型 
		 * @param name
		 * @return 
		 * 
		 */		
		public function getUnimbattleStr(name:String):String
		{
			return _tip.getColorHtml(name,"6f907f");
		}
		/**
		 *检查 是否在阵上 
		 * @param baseObj
		 * @return 
		 * 
		 */			
		public function checkObjImBattle(baseObj:ObjectData):Boolean
		{
			return (baseObj.GetPropertiesEx(IS_EMBATTLE))
		}
		/**
		 *角色排序 
		 * 
		 */		
		private function rangeRole():void
		{
			var $this:Object=this;
			var ar:Array=_tabArr[_upbattle-1];
			_roleArr.sort(compare);
			_roleArr.forEach(eachIt)
			
			function compare(v1:BaseObject,v2:BaseObject):int
			{
				if(v1 is RoleVo)
				{
					return -1
				}
				if(v2 is RoleVo)
				{
					return 1
				}
				var b:Boolean=ar.some(filte);
				var b2:Boolean=ar.some(filte2);
				if(!b&&b2)
				{
					return 1
				}
				else if(b&&!b2)
				{
					return -1
				}
				else 
				{
					return v2.GetProperties(ActorPropertyUI.LEVEL)- v1.GetProperties(ActorPropertyUI.LEVEL);
				}
				function filte(item:BaseObject,index:int,arr:Array):Boolean
				{
					return item==v1
				}
				function filte2(item:BaseObject,index:int,arr:Array):Boolean
				{
					return item==v2
				}
			}
			function eachIt(item:BaseObject,index:int,arr:Array):void
			{
				if(ar.indexOf(item)>=0)
				{
					item.HandleProperties(IS_EMBATTLE,true)
				}
				else
				{
					item.HandleProperties(IS_EMBATTLE,false);
				}
			}
		}
		private function removeAllMcBlind():void
		{
			actorGroup.getItemForEach(function(mc:MovieClip):void
			{
				mc[BIND]=_bind.UnbindProp(mc[DATAOBJ],"Level",mc[BIND]);
			})
		}
		
		
	}
}